import 'dart:io';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html_2/flutter_html_2.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/basic/local_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/widget/asr_html.dart';


class PermissionPopup {

  static String text = "欢迎您使用粮策网（以下称“我们”）的产品和服务！我们深知个人信息对您的重要性，并会尽全力保护您的个人信息安全可靠。基于此，粮策网制订《隐私政策》（以下简称“本政策”），帮助您充分了解在您使用我们的产品或服务过程中，我们会如何收集、使用、共享、存储和保护您的个人信息以及您可以如何管理您的个人信息，以便您更好地作出适当的选择。在您开始使用我们的产品和服务前，请您务必先仔细阅读和理解本政策，特别应重点阅读以粗体/粗体下划线标识的条款，确保您充分理解和同意之后再开始使用。本政策中涉及的专业词汇，我们尽量以简明通俗的表述向您解释，以便于您理解。如对本政策内容有任何疑问、意见或建议，您可通过本政策末的联系方式与我们联系。";

  static void isPermission(BuildContext context,Function agreeClick) async {
    bool isPrivacyPermission = AsrLocalInfo.isAgreePermission;
    if (isPrivacyPermission != true) {
        openPrivacyPopup(context,agreeClick);
    }else{
      if(agreeClick!=null){
        agreeClick();
      }
    }
  }

  static void openPrivacyPopup(BuildContext context,Function agreeClick){
    BuildContext that = context;
    showDialog<Null>(
        context: context, //BuildContext对象
        barrierDismissible: false,
        builder: (BuildContext context) {
          return PrivacyPopup(
            content: text,
            succFn: ()async{
              AsrLocalInfo.setIsAgreePermission();
              if(agreeClick!=null){
                agreeClick();
              }
            },
            cancelFn: (){
              openPrivacyPopup1(that,agreeClick);
            },

          );
        });
  }
  static void openPrivacyPopup1(BuildContext context,Function agreeClick){
    showDialog<Null>(
        context: context, //BuildContext对象
        barrierDismissible: false,
        builder: (BuildContext context) {
          return PrivacyPopup1(
            succFn: ()async{
              AsrLocalInfo.setIsAgreePermission();
              if(agreeClick!=null){
                agreeClick();
              }
            },
            cancelFn: (){
              exit(0);
            },
          );
        });
  }
}

class PrivacyPopup extends Dialog {
  String? content = ' ';
  Function? succFn;
  Function? cancelFn;

  PrivacyPopup({
    Key? key,
    this.succFn,
    this.content,
    this.cancelFn,
  }):super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return WillPopScope(
      onWillPop: ()async{
        return false;
      },
      child: Material(
        type: MaterialType.transparency,
        child: Scaffold(
          backgroundColor: Colors.transparent,
          body: Container(
            color: Color(0x33000000),
            child: Column(
              children: <Widget>[
                Expanded(
                  child: Container(),
                ),
                Container(
                  width: 600.w,
                  height: 1000.w,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(10)),
                      color: Colors.white),
                  padding:
                  EdgeInsets.symmetric(horizontal: 60.w, vertical: 30.w),
                  child: Column(
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(bottom: 30.w),
                        child: Text(
                          "温馨提示",
                          style: TextStyle(
                              color: Color(0xFF333333),
                              decoration: TextDecoration.none,
                              fontWeight: FontWeight.bold,
                              fontSize: 34.sp),
                        ),
                      ),
                      Expanded(
                        child: SingleChildScrollView(
                          child: Container(
                            //color: Colors.amber,
                            child: Html(data: content??'',style: {
                              'body': Style(
                                fontSize: FontSize(28.sp),
                                color: Colors.black,
                                // lineHeight: LineHeight(2.4.w),
                                backgroundColor: backgroundColor,
                              ),
                            },),
                            //child: ,
                          ),
                        ),
                      ),
                      Container(
                        child: RichText(
                          text: TextSpan(children: [
                            TextSpan(
                                text: "详细信息请完整阅读本",
                                style: TextStyle(
                                    fontSize: 26.sp,
                                    color: Color(0xFF333333))),
                            TextSpan(
                                text: "《用户协议》",
                                recognizer: TapGestureRecognizer()..onTap = (){
                                  Navigator.of(context).pushNamed('H5Page',arguments: {
                                    'url':protocolURL,
                                    'title':'《用户协议》'
                                  });
                                },
                                style: TextStyle(
                                    fontSize: 26.sp,
                                    color: Color(0xFFFF3000))),
                            TextSpan(
                                text: "《用户隐私协议》",
                                recognizer: TapGestureRecognizer()..onTap = (){
                                  Navigator.of(context).pushNamed('H5Page',arguments: {
                                    'url':privacyURL,
                                    'title':'《隐私协议》'
                                  });
                                },
                                style: TextStyle(
                                    fontSize: 26.sp,
                                    color: Color(0xFFFF3000)))
                          ]),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 30.w),
                        padding: EdgeInsets.symmetric(horizontal: 10.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: <Widget>[
                            GestureDetector(
                              onTap: () {
                                Navigator.pop(context);
                                //close();
                                if(cancelFn!=null){
                                  cancelFn!();
                                }
                              },
                              child: Text(
                                "不同意",
                                style: TextStyle(
                                  color: Color(0xFFF3F4F6),
                                  fontSize: 32.sp,
                                ),
                              ),
                            ),
                            GestureDetector(
                              onTap: () {
                                Navigator.pop(context);
                                if(succFn!=null){
                                  succFn!();
                                }
                              },
                              child: Text(
                                "同意",
                                style: TextStyle(
                                  color: Color(0xFFFC7C40),
                                  fontSize: 32.sp,
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Container(),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
class PrivacyPopup1 extends Dialog {
  String? content = "";
  Function? succFn;
  Function? cancelFn;

  PrivacyPopup1({
    Key? key,
    this.succFn,
    this.content,
    this.cancelFn,
  }):super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return WillPopScope(
      onWillPop: () async {
        return false;
      },
      child: Material(
        type: MaterialType.transparency,
        child: Scaffold(
          backgroundColor: Colors.transparent,
          body: Container(
            color: Color(0x33000000),
            child: Column(
              children: <Widget>[
                Expanded(
                  child: Container(),
                ),
                Container(
                  width: 600.w,
                  height: 450.w,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.all(Radius.circular(10)),
                      color: Colors.white),
                  padding:
                  EdgeInsets.symmetric(horizontal: 60.w, vertical: 30.w),
                  child: Column(
                    children: <Widget>[
                      Container(
                        margin: EdgeInsets.only(bottom: 30.w),
                        child: Text(
                          "温馨提示",
                          style: TextStyle(
                              color: Color(0xFF333333),
                              decoration: TextDecoration.none,
                              fontWeight: FontWeight.bold,
                              fontSize: 34.sp),
                        ),
                      ),
                      Expanded(
                        child: SingleChildScrollView(
                          child: Container(
                            //color: Colors.amber,
                            child: Html(data: "您需要同意本隐私协议才能继续，使用家庭教育规划，若您不同意本隐私协议，很遗憾我们将无法为您提供服务。"),
                            //child: ,
                          ),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(top: 30.w),
                        padding: EdgeInsets.symmetric(horizontal: 10.w),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: <Widget>[
                            GestureDetector(
                              onTap: () {
                                Navigator.pop(context);
                                //close();
                                if(cancelFn!=null){
                                  cancelFn!();
                                }
                              },
                              child: Text(
                                "取消",
                                style: TextStyle(
                                  color: Color(0xFFF3F4F6),
                                  fontSize: 32.sp,
                                ),
                              ),
                            ),
                            GestureDetector(
                              onTap: () {
                                Navigator.pop(context);
                                if(succFn!=null){
                                  succFn!();
                                }
                              },
                              child: Text(
                                "同意",
                                style: TextStyle(
                                  color: Color(0xFFFC7C40),
                                  fontSize: 32.sp,
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Expanded(
                  child: Container(),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
